package com.cy.stone.system.infrastructure.database.entity;

import cn.hutool.http.useragent.UserAgent;
import com.cy.stone.common.base.BaseEntity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Table;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 登入登出记录表 实体类。
 *
 * @author Wings
 * @since 2023-12-10
 */
@Data(staticConstructor = "create")
@Schema(name = "登入登出记录表")
@Table(value = "ST_LOGIN_RECORD")
@EqualsAndHashCode(callSuper = true)
public class LoginRecordEntity extends BaseEntity {

    /**
     * 用户名
     */
    @Schema(description = "用户名")
    @Column(value = "USER_NAME")
    private String userName;

    /**
     * 登入登出类型【1-登入 2-登出 3-被踢下线 4-被顶下线】
     */
    @Schema(description = "登入登出类型【1-登入 2-登出 3-被踢下线 4-被顶下线】")
    @Column(value = "LOGIN_TYPE")
    private String loginType;

    /**
     * IP
     */
    @Schema(description = "IP")
    @Column(value = "IP")
    private String ip;

    /**
     * 浏览器品牌
     */
    @Schema(description = "浏览器品牌")
    @Column(value = "BROWSER")
    private String browser;

    /**
     * 浏览器版本号
     */
    @Schema(description = "系统")
    @Column(value = "OS")
    private String os;

    /**
     * 设备
     */
    @Schema(description = "设备")
    @Column(value = "DEVICE")
    private String device;

    public static LoginRecordEntity loginRecord(UserAgent userAgent, String loginType, String userName, String ip) {
        LoginRecordEntity loginRecordEntity = new LoginRecordEntity();
        loginRecordEntity.setUserName(userName);
        loginRecordEntity.setLoginType(loginType);
        loginRecordEntity.setBrowser(userAgent.getBrowser().getName());
        loginRecordEntity.setOs(userAgent.getOs().getName());
        loginRecordEntity.setDevice(userAgent.isMobile() ? "mobile" : "pc");
        loginRecordEntity.setIp(ip);
        return loginRecordEntity;
    }
}
